package com.chenailin.www.service;

import com.chenailin.www.dao.StudentDAO;
import com.chenailin.www.daoimpl.StudentDAOImpl;
import com.chenailin.www.exception.ServiceException;
import com.chenailin.www.pojo.Student;

import java.sql.SQLException;

public class StudentService {
    private final StudentDAO studentDAO = new StudentDAOImpl();

    // 修改学生个人信息
    public boolean modifyStudent(Student student) {
        validateStudent(student);
        try {
            return studentDAO.modifyStudent(student);
        } catch (SQLException e) {
            throw new ServiceException("修改学生信息失败", e);
        }
    }

    // 修改密码
    public boolean modifyPassword(Student student) {
        try {
            return studentDAO.modifyPassword(student);
        } catch (SQLException e) {
            throw new ServiceException("修改密码失败", e);
        }
    }

    // 是否为有效学生 111
    private void validateStudent(Student student) throws IllegalArgumentException {
        // 姓名非空检查
        if(student.getStuName() == null || student.getStuName().trim().isEmpty()) {
            throw new IllegalArgumentException("学生姓名不能为空");
        }
    }

    // 查看学生详情
    public Student getStudentDetail(int id) {
        return studentDAO.findById(id);
    }

    // 身份验证
    public boolean verifyStudent(int id) {
        return studentDAO.verifyStudent(id);
    }

}
